
using System;
using System.Collections.Generic;

using Gtk;

using stefc.geo.data;
using stefc.geo.data.model;

using MyLocate.util;

namespace MyLocate.dlg
{

	public partial class ContactsDialog : Gtk.Dialog
	{
		
		public static bool Edit()
		{
			bool result=false;
			using(ContactsDialog dlg = new ContactsDialog())
			{	
				if((ResponseType)dlg.Run()==ResponseType.Ok)
				{
					result=true;
				}
				dlg.Destroy();
			}
			return result;
		}
	
		private ListStore listStore;
	

		public ContactsDialog ()
		{
			this.Build ();
			
			// Create a model that will hold two strings - Artist Name and Song Title
			this.listStore = new Gtk.ListStore (
				typeof(Contact), typeof (string), typeof (string));
	 
			List<Contact> contacts = DatabaseManager.Instance.ReadContacts();
			foreach(Contact contact in contacts)
				listStore.AppendValues(contact, contact.Name, contact.JId);
			
			// Assign the model to the TreeView
			this.treeview.Model = listStore;
			
			this.treeview.AppendColumn ("Name", new Gtk.CellRendererText (), "text", 1);
			this.treeview.AppendColumn ("JId", new Gtk.CellRendererText (), "text", 2);	
		}
		
		protected virtual void OnButtonCreateClicked (object sender, System.EventArgs e)
		{
			Contact contact = Contact.Create("Name", "id@domain.org");
			if(ContactProperties.Edit(contact))
			{
				DatabaseManager.Instance.UpdateContact(contact);
				this.listStore.AppendValues(contact, contact.Name, contact.JId);
			}	
		}
		
		protected virtual void OnButtonEditClicked (object sender, System.EventArgs e)
		{
			TreeIter iter;
			if(this.treeview.Selection.GetSelected(out iter))
			{
				Contact contact = (Contact) this.listStore.GetValue(iter,0);
				if(contact!=null)
				{	
					if(ContactProperties.Edit(contact))
					{
						DatabaseManager.Instance.UpdateContact(contact);
						this.listStore.SetValues(iter, contact, contact.Name, contact.JId);
					}	
				}
			}
		}
		
		
		protected virtual void OnButtonDeleteClicked (object sender, System.EventArgs e)
		{
			TreeIter iter;
			if(this.treeview.Selection.GetSelected(out iter))
			{
				Contact contact = (Contact) this.listStore.GetValue(iter,0);
				if(contact!=null)
				{
					if(DialogUtil.Question(String.Format("Kontakt '{0}' entfernen?", contact.Name)))
					{
						DatabaseManager.Instance.DeleteContact(contact);
						this.listStore.Remove(ref iter);
						this.UpdateButtonState();
					}
				}
			}
		}
		
		protected virtual void OnTreeviewRowActivated (object o, Gtk.RowActivatedArgs args)
		{
			this.UpdateButtonState();
		}
		
		private void UpdateButtonState()
		{
			TreeIter iter;
			bool rowActive = this.treeview.Selection.GetSelected(out iter);
			this.buttonDelete.Sensitive = rowActive;
			this.buttonEdit.Sensitive = rowActive;
		}
		
		protected virtual void OnTreeviewSelectCursorRow (object o, Gtk.SelectCursorRowArgs args)
		{
			this.UpdateButtonState();
		}
		
	
		
		
		
		
		
		
		/*
		 
		
		public StackWidget()
		{
			this.Build();
			
			
	 		
  			this.treeview.ModifyFont(FontDescription.FromString("Courier 10"));
		}
		
		public void Update(byte[] stack, byte sPtr)
		{
			TreeIter iter;
			if(this.stackListStore.GetIterFirst(out iter))
			{
				int index = 0xFF;
				do {
					if(index==sPtr)
						this.treeview.Selection.SelectIter(iter);
		
					this.stackListStore.SetValue(iter, 1, String.Format("{0:X2}",stack[index]));
					index--;
				} while(this.stackListStore.IterNext(ref iter));
			}
		}
	 
		  
		 */
	}
}
